package com.fy.controller;

import com.fy.dao.Ac_infoMapper;
import com.fy.dao.Forum_infoMapper;
import com.fy.dao.U_coMapper;
import com.fy.dao.ViedoMapper;
import com.fy.entity.Ac_info;
import com.fy.entity.Forum_info;
import com.fy.entity.U_co;
import com.fy.entity.Video;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/uco")
public class U_coController {
    String id = "1";
    @Resource
    Ac_infoController acInfoController;
    @Resource
    Forum_infoController forumInfoController;
    @Resource
    VideoController videoController;
    @Resource
    U_coMapper u_coMapper;


    @GetMapping("/getAll")
    public List<U_co> getAll() {
        return u_coMapper.finAll();
    }

    @PostMapping("/insert")
    public String insert(String ku_id, String info_id, String id, String type) {
        u_coMapper.insert(id, info_id, id, type);
        id = (Integer.valueOf(id) + 1 + "");
        switch (type) {
            case "ac_id":
                acInfoController.update(id, 0, 1);
                break;
            case "for_id":
                forumInfoController.update(0, 1, id);
                break;
            case "video_id":
                videoController.update(id, 0, 1);
                break;
        }
        return "200";
    }

    @PostMapping("/getInfo")
    public List<U_co> getInfo(String info_id) {
        return u_coMapper.getInfo(info_id);
    }

    @PostMapping("/delete")
    public String delete(String info_id, String id, String type) {
        u_coMapper.delete(info_id, id, type);
        switch (type) {
            case "ac_id":
                acInfoController.update(id, 0, -1);
                break;
            case "for_id":
                forumInfoController.update(0, -1, id);
                break;
            case "video_id":
                videoController.update(id, 0, -1);
                break;
        }
        return "200";
    }


    @PostMapping("/getAcInfo")
    public List<? extends List<? extends Object>> getInfo(String info_id, String type) {
        if (type.equals("ac_id")) {
            List<List<Ac_info>> acInfos = new ArrayList<>();
            List<String> id = u_coMapper.getId(info_id, type);
            for (String s : id) {
                List<Ac_info> ac_infos = acInfoController.getinfo(s);
                if (!ac_infos.isEmpty()) {
                    acInfos.add(ac_infos);
                }
            }
            return acInfos;
        }
        if (type.equals("for_id")) {
            List<List<Forum_info>> acInfos = new ArrayList<>();
            List<String> id = u_coMapper.getId(info_id, type);
            for (String s : id) {
                System.out.println(s);
                List<Forum_info> ac_infos = forumInfoController.getinfo(s);
                if (!ac_infos.isEmpty()) {
                    acInfos.add(ac_infos);
                }
            }
            return acInfos;
        }
        if (type.equals("video_id")) {
            List<List<Video>> videos = new ArrayList<>();
            List<String> id = u_coMapper.getId(info_id, type);
            for (String s : id) {
                System.out.println(s);
                List<Video> ac_infos = videoController.getinfo(s);
                if (!ac_infos.isEmpty()) {
                    videos.add(ac_infos);
                }
            }
            return videos;
        }
        return null;
    }
}
